package ywpt.service.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.XueliInfo;
import ywpt.web.common.PublicFunction;
@Service("xueliService")
@RemoteProxy(name="xueliService")
public class XueliServiceImpl extends HibernateEntityDao<XueliInfo> implements XueliService{
	private XueliServiceImpl(){
		
	}

	@Override
	public String createXueliCode() {
		DetachedCriteria query = DetachedCriteria.forClass(XueliInfo.class, "education");
		String educationCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<XueliInfo> educationInfos = this.getXueliInfos(query);
		if ( educationInfos.iterator().hasNext() )	
		{
			code = educationInfos.iterator().next().getCode();		
			maxId = new Integer(code);
		}
		educationCode=PublicFunction.getNextCode("", maxId, false,3); 
		return educationCode;
	}

	@Override
	public XueliInfo getXueliInfoById(Long id) {
		return this.get(id);
	}

	@Override
	public int getXueliInfoCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@Override
	public List<XueliInfo> getXueliInfos() {
		return this.getAll();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<XueliInfo> getXueliInfos(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<XueliInfo> getXuelifos(DetachedCriteria query, int pageSize,
			int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public void removeXueliInfo(Long id) {
		this.removeById(id);
	}

	@Override
	public void saveXueliInfo(XueliInfo XueliInfo) {
		this.save(XueliInfo);
	}

	@Override
	public void updatXueliInfo(XueliInfo XueliInfo) {
		this.update(XueliInfo);
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<XueliInfo> getXueliByXueliName(String xueliName, int start,
			int size) {
		DetachedCriteria query = DetachedCriteria.forClass(XueliInfo.class, "xueli");
		if ( xueliName != null && !xueliName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("xueliName", "%" + xueliName + "%"),  
							          Restrictions.like("code", "%" + xueliName + "%")));
		}
		query.addOrder(Order.asc("id"));
		List<XueliInfo> xueliInfoList = new ArrayList<XueliInfo>();
		List<XueliInfo> xueliInfoLists = new ArrayList<XueliInfo>();
		XueliInfo xueliInfo =new XueliInfo();
		
		xueliInfoList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<XueliInfo> itr = xueliInfoList.iterator(); itr.hasNext(); i++ ) 
		{
			xueliInfo = itr.next();
			String ss = PublicFunction.format(xueliInfo.getCode() + "",5, "  ", 1)
			          + xueliInfo.getXueliName(); 
			
			xueliInfo.setXueliName(ss);
			xueliInfoLists.add(xueliInfo);
		}
		return xueliInfoLists;
	}

	@Override
	@RemoteMethod
	public int getXuelisCountByName(String xueliName) {
		DetachedCriteria query = DetachedCriteria.forClass(XueliInfo.class, "xueli");
		if ( xueliName != null && !xueliName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("xueliName", "%" + xueliName + "%" ),  
							          Restrictions.like("code", "%" + xueliName + "%" )));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}


	
}
